package com.example.administrator.personalmail.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.example.administrator.personalmail.Activity.WaresListActivity;
import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.adapter.HomeAdapter;
import com.example.administrator.personalmail.api.Contants;
import com.example.administrator.personalmail.eventBean.HomeBanners;
import com.example.administrator.personalmail.model.Banner;
import com.example.administrator.personalmail.model.Campaign;
import com.example.administrator.personalmail.model.HomeCampaign;
import com.example.administrator.personalmail.widget.CardViewtemDecortion;
import com.example.administrator.personalmail.widget.DividerItemDecoration;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/4/14 0014.
 */

public class FragmentOne extends Fragment {

    private SliderLayout sliderShow;
    private ArrayList<Banner> mBannersList;
    private ArrayList<HomeCampaign> homeCampaignsList;
    private XRecyclerView xRecyclerView;
    private HomeAdapter homeAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one,container,false);
        sliderShow = view.findViewById(R.id.slider);

        initXrecycleview(view);
        bannerRequest();
        homeCampaignRequest();

        EventBus.getDefault().register(this);
        return view;
    }

    private void initXrecycleview(View view) {
        xRecyclerView = view.findViewById(R.id.xrv_home);
        xRecyclerView.addItemDecoration(new CardViewtemDecortion());
        xRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                homeCampaignRequest();
                xRecyclerView.refreshComplete();
            }

            @Override
            public void onLoadMore() {
//                Toast.makeText(getContext(),"没有更多内容了，亲",Toast.LENGTH_SHORT).show();
                xRecyclerView.refreshComplete();
            }
        });
    }

    private void bannerRequest() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Contants.API.HOME_BANNER)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                Log.e("---------",response.body().string());
                Gson gson = new Gson();
                Type type = new TypeToken<ArrayList<Banner>>(){}.getType();
                mBannersList = gson.fromJson(response.body().string(),type);
                EventBus.getDefault().post(new HomeBanners());
            }
        });
    }
    private void homeCampaignRequest() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Contants.API.CAMPAIGN_HOME)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                Log.e("---------",response.body().string());
                Gson gson = new Gson();
                Type type = new TypeToken<ArrayList<HomeCampaign>>(){}.getType();
                homeCampaignsList = gson.fromJson(response.body().string(),type);
                EventBus.getDefault().post(new HomeCampaign());
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getHomeBanners(HomeBanners homeBanners){
        initBanners();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getHomeCampaign(HomeCampaign homeCampaign){
        homeAdapter = new HomeAdapter(homeCampaignsList,getContext());
        homeAdapter.setHomeCampaignListenner(new HomeAdapter.HomeCampaignListenner() {
            @Override
            public void homeCampaignClick(View view, Campaign campaign) {
                Intent intent = new Intent(getActivity(), WaresListActivity.class);
                intent.putExtra("id",campaign.getId());
                startActivity(intent);
                Toast.makeText(getContext(),campaign.getId()+"",Toast.LENGTH_SHORT).show();
            }
        });
        xRecyclerView.setAdapter(homeAdapter);

    }

    private void initBanners() {
        if (mBannersList!=null){
            for (final Banner banner : mBannersList){
                TextSliderView textSliderView = new TextSliderView(getContext());
                textSliderView
                        .description(banner.getName())
                        .image(banner.getImgUrl());

                textSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                    @Override
                    public void onSliderClick(BaseSliderView slider) {
                        Toast.makeText(getContext(),banner.getName(),Toast.LENGTH_SHORT).show();
                    }
                });
                textSliderView.setScaleType(BaseSliderView.ScaleType.Fit);
                sliderShow.addSlider(textSliderView);
            }
        }
        sliderShow.setDuration(5000);
    }


    @Override
    public void onStop() {
        sliderShow.stopAutoCycle();
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
